package com.villaday.idf.custom;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import androidx.annotation.Nullable;
import android.text.Html;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.villaday.idf.R;
import com.villaday.idf.base.BaseDialog;
import com.villaday.idf.util.ToastUtil;


/**
 * 升级Svip对话框
 */
public class UpgradeSvipDialog extends BaseDialog implements View.OnClickListener {

    Context context;
    private TextView tvContent;
    private TextView tvBtn;

    public static UpgradeSvipDialog newInstance(Context context, String content, String date) {
        UpgradeSvipDialog dialog = new UpgradeSvipDialog(context);
        Bundle bundle = new Bundle();
        bundle.putString("content", content);
        bundle.putString("date", date);
        dialog.setArguments(bundle);
        return dialog;
    }

    public UpgradeSvipDialog(Context context) {
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Window window = getDialog().getWindow();
        window.requestFeature(Window.FEATURE_NO_TITLE);//必须放在setContextView之前调用
        View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_upgrade_svip, null);
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        this.setCancelable(true);
        //宽度撑满
        window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        //进入退出动画
        window.setWindowAnimations(R.style.animTranslateTop);

        tvBtn = (TextView) view.findViewById(R.id.btn_confirm);
        ImageView ivClose = (ImageView) view.findViewById(R.id.iv_close_dialog);
        tvContent = (TextView) view.findViewById(R.id.tvVipLevel);
        if (TextUtils.isEmpty(getArguments().getString("date"))) {
            tvContent.setText(Html.fromHtml(getArguments().getString("content") + String.format(getResources().getString(R.string.textZD), "")));
        } else {
            tvContent.setText(Html.fromHtml(getArguments().getString("content") + String.format(getResources().getString(R.string.textZD), "(" + getArguments().getString("date") + ")")));
        }

        tvBtn.setOnClickListener(this);
        ivClose.setOnClickListener(this);
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            DisplayMetrics dm = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            dialog.getWindow().setLayout((int) (dm.widthPixels * 0.85), ViewGroup.LayoutParams.WRAP_CONTENT);
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_confirm) {
            onItemClickListener.onItemClick(v);
            dismiss();
        } else if (v.getId() == R.id.iv_close_dialog) {
            dismiss();
        }
    }

}
